//任务状态控制js
$(function(){
    setInterval(function(){
        updateClientStatus();
        updateTaskStatus();
    }, 1000);
});

/**
 * 更新终端状态
 */
function updateClientStatus(){
    //获得终端列表
    var clientList = $("[id^='tr_client_']");

    var uids = [];
    if(clientList && clientList.length > 0){
        for(var i = 0; i < clientList.length; i++){
            uids.push(clientList[i].getAttribute("uid"));
        }

        myajax({
            url: "/task/queryClientStatus",
            data: {"uids": uids},
            success: function(data){
                for(var i = 0; i < data.length; i++){
                    //获得当前任务id
                    var dinfo = data[i];

                    //从结果中获取任务id
                    var uid = dinfo.uid;
                    //获得当前任务的状态
                    var status = dinfo.status;
                    //获得当前播放歌曲
                    var mp3 = dinfo.mp3;

                    //设置状态
                    var html = "";
                    if(status == 0 || status == 2){
                        html += "任务空闲";
                    } else if(status == 1){
                        html += "执行中";
                    } else if(status == 3){
                        html += "暂停";
                    }
                    $("#client_status_" + uid).html(html);

                    //设置歌曲
                    $("#client_mp3_" + uid).html(mp3);
                }
            }
        });
    }
}

/**
 * 更新任务状态
 */
function updateTaskStatus(){
    //获得任务列表
    var taskList = $("[id^='tr_task_']");

    var taskids = [];
    if(taskList && taskList.length > 0){
        for(var i = 0; i < taskList.length; i++){
            taskids.push(taskList[i].getAttribute("taskidstr"));
        }

        myajax({
            url: "/task/queryTaskStatus",
            data: {"taskids": taskids},
            success: function (data) {
                for(var i = 0; i < data.length; i++){
                    //获得当前任务id
                    var dinfo = data[i];

                    //从结果中获取任务id
                    var taskid = dinfo.taskid;
                    //获得当前任务的状态
                    var status = dinfo.status;
                    //获得当前播放歌曲
                    var mp3 = dinfo.mp3;
                    //获得当前持续时间
                    var duration = dinfo.duration;
                    //获得当前任务类型
                    var type = dinfo.type;
                    //获得当前更新时间
                    var startDate = dinfo.startDate;
                    //获得当前进度
                    var curtime = dinfo.curtime;
                    //获得任务总时长
                    var allduration = dinfo.allduration;

                    //设置状态
                    if(typeof(status) != "undefined"){
                        var html = "";
                        if(status == 0 || status == 2){
                            html += "任务空闲";

                            $("#task_mp3_" + taskid).html("");
                            $("#task_allduration_" + taskid).html(s2str(0));
                            $("#task_nowduration_" + taskid).html(s2str(0));
                            $("#task_duration_" + taskid).html(s2str(0));
                        } else if(status == 1){
                            html += "执行中";

                            //设置歌曲
                            if(mp3){
                                $("#task_mp3_" + taskid).html(mp3);
                            }

                            //设置任务总进度
                            if(allduration) {
                                $("#task_allduration_" + taskid).html(s2str(allduration));
                            }

                            //设置当前进度
                            if(curtime){
                                $("#task_nowduration_" + taskid).html(s2str(curtime));
                            }

                            //设置当前曲目时长
                            if(duration){
                                $("#task_duration_" + taskid).html(s2str(duration));
                            }

                        } else if(status == 3){
                            html += "暂停";
                        }
                        $("#task_status_" + taskid).html(html);
                    }

                    //当前一次性任务已经停止
                    if(parseInt(type) == 3 && parseInt(status) == 2){
                        //删除一次性任务
                        $("[id^='tr_task_'][taskidstr='" + taskid + "']").remove();
                    } else if(startDate){
                        //更新开始时间
                        $("#task_startdate_" + taskid).html(startDate);
                    }
                }
            }
        });
    }
}

/**
 * 秒转字符串
 * @param secoendTime
 */
function s2str(secoendTime){
    var str = "";
    var m = parseInt(secoendTime/60);//获得多少分钟

    if(m < 10){
        str += "0" + m;
    } else {
        str += m;
    }

    str += ":";

    var s = parseInt(secoendTime%60);//获得多少秒
    if(s < 10){
        str += "0" + s;
    } else {
        str += s;
    }

    return str;
}